var User = function() {
    var reg_phone = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    var reg_email = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
    var canSend = true;
    var base_url= "";
    var getCode = function(){
        $('.js-getCode').on('click',function(){
            if (!canSend) return;
            var phone = $('#phone').val();
            if(!reg_phone.test(phone)){
                $('.js-phone').text("请输入正确的手机号码").removeClass('hide');
                return;
            }else{
                if(!$('.js-phone').hasClass('hide')){
                    $('.js-phone').text("").addClass('hide');
                }
            }
            $.ajax({
                type: 'POST',
                url: base_url+apis.code_url,
                data: {phone:phone},
                dataType:"json",
                success: function (result) {
                    if(result.code==1){
                        //禁用获取短信验证码按钮
                        var _this = $(".js-getCode");
                        //显示60秒倒计时
                        var seconds = 60;
                        var text = _this.text();
                        _this.val( "(" + seconds + ")");
                        var interval = window.setInterval(function(){
                            seconds --;
                            if (seconds == 0){
                                canSend = true;
                                window.clearInterval(interval);
                                _this.bind('click',getCode);
                                _this.text(text);
                            }else{
                                canSend = false;
                                _this.text("(" + seconds + ")");
                            }
                        }, 1000);
                    }else{
                        alert("获取短信失败，请于管理员联系");
                    }
                },
                fail:function(){

                }
            })
        })
    }
    var login   = function(){
        $('.js-login').on("click",function(){
            var phone = $('#phone').val();
            var code  = $('#code').val();
            if(!reg_phone.test(phone)){
                $('.js-phone').text("请输入正确的手机号码").removeClass('hide');
                return;
            }else{
                if(!$('.js-phone').hasClass('hide')){
                    $('.js-phone').text("").addClass('hide');
                }
            }
            if(!code || code == "undefine" || code==null ){
                $('.js-code').text("请输入验证码").removeClass('hide');
                return;
            }else{
                if(!$('.js-code').hasClass('hide')){
                    $('.js-code').text("").addClass('hide');
                }
            }
            $.ajax({
                type: 'POST',
                url: base_url+apis.login_url,
                data: {phone:phone,code:code},
                dataType:"json",
                success: function (result) {
                    if(result.code==1){
                        window.location.href = base_url
                    }else{
                        alert("手机号码或验证码错误！");
                    }
                },
                fail:function(){

                }
            })
        })


    }
    var userAdd = function(baseUrl) {
        $('.js-add').on('click',function(){
            var data     = {},
                phone    = $('#add_phone').val(),
                username = $('#add_username').val(),
                email    = $('#add_email').val(),
                company  = $('#add_company').val();
            if(!reg_phone.test(phone)){
                $('.js-add-phone').text("请输入正确的手机号码").removeClass('hide');
                return;
            }else{
                if(!$('.js-add-phone').hasClass('hide')){
                    $('.js-add-phone').text("").addClass('hide');
                }
            }
            if(!username || username ==="undefine" || username==null){
                $('.js-add-username').text("请输入用户名").removeClass('hide');
                return;
            }else{
                if(!$('.js-add-username').hasClass('hide')){
                    $('.js-add-username').text("").addClass('hide');
                }
            }
            if(!reg_email.test(email)){
                $('.js-add-email').text("请输入正确的邮箱地址").removeClass('hide');
                return;
            }else{
                if(!$('.js-add-email').hasClass('hide')){
                    $('.js-add-email').text("").addClass('hide');
                }
            }
            if(!company || company ==="undefine" || company==null){
                $('.js-add-company').text("请输入公司名称").removeClass('hide');
                return;
            }else{
                if(!$('.js-add-company').hasClass('hide')){
                    $('.js-add-company').text("").addClass('hide');
                }
            }
            data.phone    = phone;
            data.email    = email;
            data.username = username;
            data.company  = company;
            $.ajax({
                type: 'POST',
                url: base_url+apis.userStore_url,
                data: data,
                success: function (result) {
                    result = JSON.parse(result);
                    if(result.code==1){
                        alert("添加成功");
                        $('#ModalUser').modal("hide");
                        window.location.reload();
                    }else{
                        alert(result.msg);
                    }
                },
                fail:function(){

                }
            })

        })
    }
    var userEdit = function(baseUrl){
        $('.js-edit').on('click',function(){
            $('#userid').val($(this).parent().parent().data('id'));
            $('#edit_phone').val($(this).parent().siblings().eq(3).text());
            $('#edit_username').val($(this).parent().siblings().eq(1).text());
            $('#edit_email').val($(this).parent().siblings().eq(2).text());
            $('#edit_company').val($(this).parent().siblings().eq(4).text());
            $('#edit_status').val($(this).parent().siblings().eq(5).data("id"));
            $("#ModalEdit").modal("show");
        })

        $('.js-save').on('click',function(){

            var data     = {},
                userid   = $('#userid').val(),
                username = $('#edit_username').val(),
                email    = $('#edit_email').val(),
                company  = $('#edit_company').val(),
                status   = $('#edit_status').val();
            if(!username || username ==="undefine" || username==null){
                $('.js-edit-username').text("请输入用户名").removeClass('hide');
                return;
            }else{
                if(!$('.js-edit-username').hasClass('hide')){
                    $('.js-edit-username').text("").addClass('hide');
                }
            }
            if(!reg_email.test(email)){
                $('.js-edit-email').text("请输入正确的邮箱地址").removeClass('hide');
                return;
            }else{
                if(!$('.js-edit-email').hasClass('hide')){
                    $('.js-edit-email').text("").addClass('hide');
                }
            }
            if(!company || company ==="undefine" || company==null){
                $('.js-edit-company').text("请输入公司名称").removeClass('hide');
                return;
            }else{
                if(!$('.js-edit-company').hasClass('hide')){
                    $('.js-edit-company').text("").addClass('hide');
                }
            }
            data.userid   = userid;
            data.email    = email;
            data.username = username;
            data.company  = company;
            data.status   = status;
            $.ajax({
                type: 'POST',
                url: base_url+apis.userStore_url,
                data: data,
                success: function (result) {
                    result = JSON.parse(result);
                    if(result.code==1){
                        alert("保存成功");
                        $('#ModalEdit').modal("hide");
                        window.location.reload();
                    }else{
                        alert(result.msg);
                    }
                },
                fail:function(){

                }
            })

        })
    }
    return {
        //main function to initiate template pages
        init: function (baseUrl) {
            base_url = baseUrl;
            getCode();
            login();
            userAdd();
            userEdit();

        }

    }
}();